<template>
	<view class="members-container">
		<!-- 邀请入队按钮 -->
		<view class="invite-section" @tap="handleInvite">
			<view class="invite-btn">
				<text class="plus-icon">+</text>
				<text>邀请入队</text>
			</view>
		</view>

		<!-- 成员分类标签 -->
		<view class="member-tabs">
			<view class="tab-item active">
				<text class="tab-text">球队成员({{members.length}})</text>
			</view>
			<!-- <view class="tab-item">
				<text class="tab-text">球队外援(0)</text>
			</view> -->
		</view>

		<!-- 成员列表 -->
		<view class="members-list">
			<!-- 队长 -->
			<view class="member-item" v-for="(member,index ) in members" :key="member.id">
				<view class="member-role">
					{{member.position||'暂无位置'}}
					<text class="number">{{member.number ? `（${member.number}号）` : '(暂无号码)'}}</text>
				</view>
				<view class="member-info">
					<image class="avatar" :src="member.sysUser.avatar" mode="aspectFill"
						@tap.stop="goToUserProfile(member.sysUser.userId)" />
					<view class="info">
						<text class="nickname">{{member.sysUser.nickName}}</text>
						<text class="activity">入队时间:{{member.joinTime}}</text>
					</view>
				</view>
				<text class="member-status" @tap="goToMemberSetting(member)">{{member.role}} ></text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		listMembers
	} from '@/api/youqiu/members.js'
	export default {
		data() {
			return {
				teamId: '',
				teamName: '',
				members: [],
				shareTeam: null,
				userInfo: {}
			}
		},
		onLoad(options) {
			const storage_data = uni.getStorageSync('storage_data')
			if (storage_data && storage_data.vuex_user_info) {
				this.userInfo = storage_data.vuex_user_info
			} else {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login'
					})
				}, 500)
				return
			}

			if (options.teamId) {
				this.teamId = options.teamId
				this.teamName = options.teamName
				this.listMembers()
			}
		},
		onShareAppMessage(res) {
			if (!this.userInfo.userId) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				})
				return {
					title: '加入球队',
					path: 'pages/index/index'
				}
			}

			return {
				title: `邀请加入${this.teamName}`,
				path: `pages/team/invite/index?teamId=${this.teamId}&inviterId=${this.userInfo.userId}&inviterName=${this.userInfo.nickName}`,
				imageUrl: '/static/images/invite-default.jpg'
			}
		},
		methods: {
			listMembers() {
				let _this = this
				const query = {
					teamId: this.teamId,
					pageSize: 1000,
					pageNum: 1
				}
				listMembers(query).then(res => {
					console.log("listMembers", res)
					if (res.code === 200) {
						_this.members = res.rows || []
					} else {
						_this.members = []
						uni.showToast({
							title: res.msg || '获取成员列表失败',
							icon: 'none'
						})
					}
				})

			},
			handleInvite() {
				if (!this.userInfo.userId) {
					uni.showToast({
						title: '请先登录',
							icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login'
						})
					}, 500)
					return
				}

				uni.showShareMenu({
					withShareTicket: true,
					menus: ['shareAppMessage']
				})

				uni.showToast({
					title: '点击右上角分享',
					icon: 'none',
					duration: 2000
				})
			},
			goToMemberSetting(member) {
				uni.navigateTo({
					url: `/pages/team/member-setting?teamId=${this.teamId}&id=${member.id}`,
					success: (res) => {
						if (res.eventChannel) {
							res.eventChannel.emit('memberInfo', member)
						}
					}
				})
			},
			goToUserProfile(userId) {
				if (!userId) {
					uni.showToast({
						title: '用户信息不存在',
						icon: 'none'
					})
					return
				}

				uni.navigateTo({
					url: `/pages/profile/detail?userId=${userId}`,
					fail: (err) => {
						console.error('跳转用户主页失败:', err)
						uni.showToast({
							title: '页面跳转失败',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.members-container {
		min-height: 100vh;
		background: #f5f5f5;
	}

	.invite-section {
		background: #fff;
		padding: 30rpx 20rpx;
		margin-bottom: 20rpx;

		.invite-btn {
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx;
			background: #f0f9f0;
			border-radius: 8rpx;
			border: 1rpx dashed #4CAF50;

			.plus-icon {
				font-size: 32rpx;
				color: #4CAF50;
				margin-right: 12rpx;
				line-height: 1;
				height: 32rpx;
				display: flex;
				align-items: center;
			}

			text {
				font-size: 28rpx;
				color: #4CAF50;
				line-height: 1;
				height: 28rpx;
				display: flex;
				align-items: center;
			}
		}
	}

	.member-tabs {
		display: flex;
		background: #fff;
		padding: 0 20rpx;
		margin-bottom: 20rpx;

		.tab-item {
			position: relative;
			padding: 20rpx 30rpx;
			margin-right: 40rpx;

			&.active::after {
				content: '';
				position: absolute;
				left: 0;
				bottom: 0;
				width: 100%;
				height: 4rpx;
				background: #ff6b35;
			}

			.tab-text {
				font-size: 28rpx;
				color: #333;
			}
		}
	}

	.members-list {
		background: #fff;
		padding: 0 20rpx;

		.member-item {
			display: flex;
			align-items: center;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #eee;

			.member-role {
				font-size: 24rpx;
				color: #666;
				margin-right: 20rpx;
				min-width: 120rpx;
				display: flex;
				flex-direction: row;
				align-items: flex-start;
				flex-wrap: wrap;
				line-height: 1.5;

				.number {
					color: #999;
					margin-left: 4rpx;
				}
			}

			.member-info {
				flex: 1;
				display: flex;
				align-items: center;

				.avatar {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					margin-right: 20rpx;
					cursor: pointer;

					&:active {
						opacity: 0.8;
					}
				}

				.info {
					display: flex;
					flex-direction: column;

					.nickname {
						font-size: 28rpx;
						color: #333;
						margin-bottom: 4rpx;
					}

					.activity {
						font-size: 24rpx;
						color: #999;
					}
				}
			}

			.member-status {
				font-size: 24rpx;
				color: #999;
			}
		}
	}
</style>